package com.github.springboot.exception;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.springboot.common.Response;
import org.apache.log4j.Logger;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by kjtang on 2018/1/18.
 */
public class ApplicationExceptionResolver implements HandlerExceptionResolver {

    private Logger logger = Logger.getLogger(getClass());

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object handler, Exception ex) {
        logger.info("异常拦截器执行开始。");
        // 输出 异常信息
        String msgContent = "";
        logger.error("发生异常:" + msgContent, ex);
        // 将异常信息转json输出
        logger.info("异常拦截器执行结束。");
        return new ModelAndView();
    }

//    @ExceptionHandler({ Exception.class })
//    public @ResponseBody
//    Response processControllerException(NativeWebRequest request, Exception e) {
//        Response response = new Response(Response.FAIL);
//        if (e instanceof ErrorRCodeException) {
//            response.setRcode(((ErrorRCodeException) e).getRcode());
//        } else {
//            logger.error(e.getMessage(), e);
//            response.setRcode(SysConstants.RCode.INTERNAL_SERVER_ERROR);
//        }
//        return response;
//    }

}
